<?php
/*--------------------------------
 * 华兴软通接口
功能:		PHP HTTP接口 发送短信
*/

function sendSMS($username, $password, $mobile, $content, $apikey)
{
	$url = 'http://www.stongnet.com/sdkhttp/sendsms.aspx';
	$data = array(
		'reg'=>$username,					//用户账号
		'pwd'=>$password,				//密码
		'sourceadd' => "",
		'phone'=>$mobile,					//号码
		'content'=>$content				//内容
	);
	$result= curlSMS($url,$data);			//POST方式提交
	$result_array = strToArray($result);
	if($result["result"] == "0")
		throw new Exception("短信验证码发送失败，错误原因：".$result);
}

function curlSMS($url,$post_fields=array()){
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL,$url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch, CURLOPT_TIMEOUT, 3600); //60秒 
	curl_setopt($ch, CURLOPT_HEADER,1);
	curl_setopt($ch, CURLOPT_REFERER,'http://www.yourdomain.com');
	curl_setopt($ch, CURLOPT_POST,1);
	curl_setopt($ch, CURLOPT_POSTFIELDS,$post_fields);
	$data = curl_exec($ch);
	curl_close($ch);
	$res = $data;
	return $res; 
}

function strToArray($param){
	$arr = explode('&',$str);
	for($i=0;$i<count($arr);$i++){
		$arrs[] = explode('=',$arr[$i]);
	}
	foreach($arrs as $k=>$v){
		foreach($v as $ks=>$vs){
			$arrvs[] = $vs;
		}
	}
	for($i=0;$i<count($arrvs);$i++){
		if($i%2 == 0){
			$kss[] = $arrvs[$i];
			$vss[] = $arrvs[$i+1];
		}
	}
	return $kv = array_combine($kss,$vss);
}

?>